package com.ruoyi.web.controller.performance;

import com.etoppaas.performance.domain.PerTemplate;
import com.etoppaas.performance.domain.PerTemplateSpecRelation;
import com.etoppaas.performance.service.IPerTemplateService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  信息操作处理
 * 
 * @author ruoyi
 * @date 2019-06-05
 */
@Controller
@RequestMapping("/performance/perTemplate")
public class PerTemplateController extends BaseController
{
    private String prefix = "performance/perTemplate";
	
	@Autowired
	private IPerTemplateService perTemplateService;
	
	@RequiresPermissions("performance:perTemplate:view")
	@GetMapping()
	public String perTemplate()
	{
	    return prefix + "/perTemplate";
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("performance:perTemplate:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(PerTemplate perTemplate)
	{
		startPage();
        List<PerTemplate> list = perTemplateService.selectPerTemplateList(perTemplate);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出列表
	 */
	@RequiresPermissions("performance:perTemplate:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PerTemplate perTemplate)
    {
    	List<PerTemplate> list = perTemplateService.selectPerTemplateList(perTemplate);
        ExcelUtil<PerTemplate> util = new ExcelUtil<PerTemplate>(PerTemplate.class);
        return util.exportExcel(list, "perTemplate");
    }
	
	/**
	 * 新增
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存
	 */
	@RequiresPermissions("performance:perTemplate:add")
	@Log(title = "", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(PerTemplate perTemplate)
	{		
		return toAjax(perTemplateService.insertPerTemplate(perTemplate));
	}

	/**
	 * 修改
	 */
	@GetMapping("/edit/{templateId}")
	public String edit(@PathVariable("templateId") Long templateId, ModelMap mmap)
	{
		PerTemplate perTemplate = perTemplateService.selectPerTemplateById(templateId);
		mmap.put("perTemplate", perTemplate);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存
	 */
	@RequiresPermissions("performance:perTemplate:edit")
	@Log(title = "", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(PerTemplate perTemplate)
	{		
		return toAjax(perTemplateService.updatePerTemplate(perTemplate));
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("performance:perTemplate:remove")
	@Log(title = "", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(perTemplateService.deletePerTemplateByIds(ids));
	}

	/**
	 * 模板配置
	 */
	@GetMapping("/config/{templateId}")
	public String config(@PathVariable("templateId") Long templateId, ModelMap mmap)
	{
		PerTemplate perTemplate = perTemplateService.selectPerTemplateById(templateId);
		mmap.put("perTemplate", perTemplate);
		mmap.put("templateId",templateId);
		return prefix + "/config";
	}
	/**
	 * 模板配置
	 */
	@GetMapping("/distributeWeight/{templateId}")
	public String distributeWeight(@PathVariable("templateId") Long templateId, ModelMap mmap)
	{
		PerTemplate perTemplate = perTemplateService.selectPerTemplateById(templateId);
		List<PerTemplateSpecRelation> relations= perTemplateService.getDistributeData(templateId);
		mmap.put("relations",relations);
		mmap.put("perTemplate", perTemplate);
		mmap.put("templateId",templateId);
		return prefix + "/distributeWeight";
	}

	@GetMapping("/checkWeightLegal/{templateId}")
	@ResponseBody
	public AjaxResult checkWeightLegal(@PathVariable("templateId")Long templateId){
		return perTemplateService.checkWeightLegal(templateId);
	}
}
